// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#include "serde.h"
#include "context.h"

#include <pollux/vector/vector_saver.h>

namespace kumo::pollux::py {

namespace py = pybind11;

namespace {
VectorPtr pyRestoreVectorFromFileHelper(const char* filePath) {
  using namespace kumo::pollux;
  memory::MemoryPool* pool = PyPolluxContext::getSingletonInstance().pool();
  return restoreVectorFromFile(filePath, pool);
}
} // namespace

void addSerdeBindings(py::module& m, bool asModuleLocalDefinitions) {
  using namespace kumo::pollux;

  m.def(
      "save_vector",
      &saveVectorToFile,
      R"delimiter(
        Serializes the vector into binary format and writes it to a new file.

        Parameters
        ----------
        vector : Union[FlatVector, ConstantVector, DictionaryVector]
              The vector to be saved.
        file_path: str
              The path to which the vector will be saved.

        Returns
        -------
        None

        Examples
        --------

        >>> import pypollux as pv
        >>> vec = pv.from_list([1, 2, 3])
        >>> pv.save_vector(vec, '/tmp/flatvector.bin')
      )delimiter",
      py::arg("vector"),
      py::arg("file_path"));
  m.def(
      "load_vector",
      &pyRestoreVectorFromFileHelper,
      R"delimiter(
        Reads and deserializes a vector from a file stored by save_vector.

        Parameters
        ----------
        file_path: str
              The path from which the vector will be loaded.

        Returns
        -------
        Union[FlatVector, ConstantVector, DictionaryVector]

        Examples
        --------

        >>> import pypollux as pv
        >>> pv.load_vector('/tmp/flatvector.bin')
        <pypollux.FlatVector_BIGINT object at 0x7f8f6f818bb0>
      )delimiter",
      py::arg("file_path"));
}

} // namespace kumo::pollux::py
